/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.context.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.genia.toolbox.web.context.WebContext;
import com.genia.toolbox.web.helper.TrampolineWebUtils;

/**
 * basic implementation of <code>WebContext</code>.
 */
public class WebContextImpl
    implements WebContext
{

  /**
   * retrieve the current <code>HttpServletRequest</code>.
   * 
   * @return the current <code>HttpServletRequest</code>
   */
  public HttpServletRequest getRequest()
  {
    final RequestAttributes currentRequestAttributes = RequestContextHolder.currentRequestAttributes();
    if (currentRequestAttributes == null) {
      return null;
    }
    if (currentRequestAttributes instanceof ServletRequestAttributes) {
      return ((ServletRequestAttributes) currentRequestAttributes).getRequest();
    }
    return null;
  }



  /**
   * retrieve the current <code>HttpSession</code>.
   * 
   * @return the current <code>HttpSession</code>
   */
  public HttpSession getSession()
  {
    return getRequest().getSession();
  }



  /**
   * Return the relative path associated with the current servlet.
   * 
   * @return the relative path associated with the current servlet
   * @see com.genia.toolbox.web.context.WebContext#getRelativePath()
   */
  public String getRelativePath()
  {
    return TrampolineWebUtils.getRelativePath(getRequest());
  }

}
